#import "BWPartialStitchTool.h"
#import "BWCrossStitchChangeList.h"
#import "BWCrossStitchSet.h"
#import "BWLayerSet.h"
#import "BWGridGeometry.h"
#import "BWCrossStitchLayer.h"
#import "BWLayerView.h"

#import "BWColorKeyWindowController.h"

@implementation BWPartialStitchTool

- (id) initWithGeometry: (BWGridGeometry *) g
                 layers: (BWLayerSet *) l
              layerView: (BWLayerView *) lv
              direction: (BWPartialStitchTool_Direction) d
{
    if (self = [super initWithGeometry: g
                      layers: l
                      layerView: lv]) {
        direction = d;
    }

    return (self);

} // initWithGeometry


- (void) addChangeAtRow: (int) row
                 column: (int) column
                 thread: (BWThread *) thread
{
    // see what's already there
    BWCrossStitchSet *set = [layer crossStitches];
    BWCrossStitch *existingStitch = nil;
    [set getSymbol: &existingStitch  atRow: row  column: column];

    BWThread *existingTopThread = nil, *existingBottomThread = nil;

    if ([existingStitch divider] == kDivider_None) {
        existingTopThread = existingBottomThread = [existingStitch topThread];
    } else {
        existingTopThread = [existingStitch topThread];
        existingBottomThread = [existingStitch bottomThread];
    }

    BWThread *topThread = nil, *bottomThread = nil;
    BWThread_StitchDivider divider = kDivider_Forward;

    switch (direction) {
    case kDirection_NW:
        topThread = thread;
        bottomThread = existingBottomThread;
        divider = kDivider_Forward;
        break;

    case kDirection_NE:
        topThread = thread;
        bottomThread = existingBottomThread;
        divider = kDivider_Backward;
        break;

    case kDirection_SW:
        topThread = existingTopThread;
        bottomThread = thread;
        divider = kDivider_Backward;
        break;

    case kDirection_SE:
        topThread = existingTopThread;
        bottomThread = thread;
        divider = kDivider_Forward;
        break;
    }

    BWCrossStitch *stitch;
    stitch = [[BWCrossStitch alloc]
                 initWithRow: row
                 column: column
                 topThread: topThread
                 bottomThread: bottomThread
                 divider: divider
                 tag: kCrossStitchChange_add];

    [changeList addStitch: stitch];

    [stitch release];

} // addChangeAtRow



- (void) startDragAtRow: (int) row  
                 column: (int) column
                 thread: (BWThread *) thread
{
    [self addChangeAtRow: row  column: column  thread: thread];

    [self invalidateRow: row  column: column];

} // handleMouseDown


- (void) continueDragAtRow: (int) row
                    column: (int) column
                    thread: (BWThread *) thread
{
    [self addChangeAtRow: row  column: column  thread: thread];

    [self invalidateRow: row  column: column];

} // handleMouseDragged



- (NSString *) name
{
    return (@"Draw partial stitches");
} // name

@end // BWPartialStitchTool

